import { basicBracketEnum, quoteBracketEnum } from "../enum";
import type { BracketInfoType, BracketLeftType, BracketRightType, BracketType, NumberStackType } from "./types";

class Bracket implements BracketType {
    #bracketInfo: BracketInfoType;
    action: Function | undefined;
    constructor(bracketInfo: BracketInfoType, actionFn?:Function) {
        this.#bracketInfo = bracketInfo;
        this.action = actionFn;
    }
    getBasic(): basicBracketEnum {
        return this.#bracketInfo.basic;
    }
    getQuote(): quoteBracketEnum {
        return this.#bracketInfo.quote;
    }
}

export class BracketLeft extends Bracket implements BracketLeftType{
    bracketName: basicBracketEnum.left = basicBracketEnum.left;
}

export class BracketRight extends Bracket implements BracketRightType{
    bracketName: basicBracketEnum.right = basicBracketEnum.right;
}

export class NumberStack implements NumberStackType {
    #numberArray: Array<number>;
    constructor(initArray?:Array<number>) {
        this.#numberArray = initArray || [];
    }
    pushNewIndex(index: number): void {
        this.#numberArray.push(index);
    }
    popPeekIndex(): number | undefined {
        return this.#numberArray.pop();
    }
    clearAllIndex(): void {
        this.#numberArray = [];
    }
    countSize(): number {
        return this.#numberArray.length;
    }
    countBeforeItemOf(targetIndex: number): number {
        const locationArr: number[] = this.#numberArray;
        let record: number = 0;
        // 属性遍历括号索引数组，返回第一个索引值大于等于目标索引前面的元素个数
        for (let i = 0; i < locationArr.length; i++) {
            if (targetIndex >= locationArr[i]) {
                record++;
            } else {
                break;
            }
        }
        return record;
    }
}